/*
 * Copyright 2008 Wink Saville
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.saville.msgcomp;

import com.saville.serdes.*;
import java.util.concurrent.atomic.*;

public class DevId implements ISerdes<DevId> {
    public static final int SERDES_ID = 4;
    public static final String SERDES_ID_STRING = "4";

    public long id;
    public String urn;
    public String friendlyName;

    @Override
    public String toString() {
        StringBuilder s = new StringBuilder();

        s.append(" id=");
        s.append(id);
        s.append(" urn=");
        s.append(urn);
        s.append(" friendlyName=");
        s.append(friendlyName);

        return s.toString();
    }

    public String serdesIdString() {
        return SERDES_ID_STRING;
    }

    @Override
    public int serdesId() {
        return SERDES_ID;
    }

    public ISerdes<DevId> make() {
        return new DevId();
    }

    @Override
    public void serialize(SerdesBinary serdes) {
        serdes.writeInt(serdesId());
        serdes.writeLong(id);
        serdes.writeString(urn);
        serdes.writeString(friendlyName);
    }

    @Override
    public DevId deserialize(SerdesBinary serdes) {
        id = serdes.readLong();
        urn = serdes.readString();
        friendlyName = serdes.readString();

        return this;
    }

    @Override
    public void serialize(SerdesJson serdes) {
        try {
            serdes.startObject();
            serdes.writeKey(serdesIdString());
            serdes.startArray();
            serdes.writeLong(id);
            serdes.writeString(urn);
            serdes.writeString(friendlyName);
            serdes.endArray();
            serdes.endObject();
        } catch (Exception e) {
            throw new RuntimeException("DevId#serialize json exception");
        }
    }

    @Override
    public DevId deserialize(SerdesJson serdes) {
        try {
            char c = serdes.nextChar();
            if (c != '[') {
                throw new RuntimeException("DevId#deserialize json expecting '['");
            }
            id = serdes.readLong();
            urn = serdes.readString();
            friendlyName = serdes.readString();
            c = serdes.nextChar();
            if (c != ']') {
                throw new RuntimeException("DevId#deserialize json expecting ']'");
            }
        } catch (Exception e) {
            // FXIME: Best way to handle nested throws so as not to hide?
            throw new RuntimeException("DevId#deserialize json exception");
        }
        return this;
    }


    /*
     * This code should be present in all classes that implement ISerdes
     */
    {
        if (registered.compareAndSet(0, 1)) {
            SerdesBinary.register(serdesId(), this);
            SerdesJson.register(serdesId(), this);
        }
    }

    private static final AtomicInteger registered = new AtomicInteger();
}
